#!/bin/sh
# Created by yuzhengyang
# 用于部署ureport服务

basePath=/home/urpt
# 检测模块是否启动完成
function heartbeat(){
  case "$1" in
  7090)
     while ((1))
     do
        sleep 2
        platform=`netstat -anp | grep :$1 | grep -v grep`
        if [ "$platform" != "" ]; then
           echo "=================================================="
           echo "platform 服务启动成功，端口：7090"
           echo "=================================================="
           break
        else
           echo ">>> platform 服务正在启动......"
        fi
     done ;;

  7091)
     while ((1))
     do
        sleep 2
        ureport=`netstat -anp | grep :$1 | grep -v grep`
        if [ "$ureport" != "" ]; then
           echo "=================================================="
           echo "ureport 服务启动成功，端口：7091"
           echo "=================================================="
           break
        else
           echo ">>> ureport 服务正在启动......"
        fi
     done ;;


  esac
}

# 更新模块 jar 包，并启动对应模块项目
function startProcess(){
   case "$1" in
    7090)
     # 更新 web 模块 jar 包
     cp -rf ${basePath}/update/platform-n-1.0.2.jar ${basePath}/platform/
     # 启动 web 模块
     cd ${basePath}/platform &&  nohup java -jar platform-n-1.0.2.jar --server.port=7090 --spring.config.location=application.yml > ./nohup_output &
     # 检测模块是否已启动完成
     heartbeat 7090

     ;;
    7091)
     # 更新 web 模块 jar 包
     cp -rf ${basePath}/update/ureport-1.0.2.jar ${basePath}/ureport/
     # 启动 web 模块
     cd ${basePath}/ureport &&  nohup java -jar ureport-1.0.2.jar --server.port=7091 --spring.config.location=application.yml > ./nohup_output &
     # 检测模块是否已启动完成
     heartbeat 7091

     # fi
     ;;
   esac
}


# 运行模块进程
function runProcess(){
   if [ $# -eq 1 ]; then
		# 停止指定模块进程
		netstat -anp | grep :$1 |awk '{print $7}'|awk -F"/" '{print $1}'|xargs kill -9
		echo "==> The $1 port has been closed."
		# 启动指定模块进程
		startProcess $1
   else
      ports=(7090 7091)
      for port in ${ports[*]}
      do
        # 停止每个模块进程
        netstat -anp | grep :$port |awk '{print $7}'|awk -F"/" '{print $1}'|xargs kill -9
        echo "==>The $port port has been closed."
        # 启动每个模块进程
        startProcess $port
      done

	  # echo "==> 开始更新web页面和webChat页面程序..."
	  # cp -rf ${basePath}/update/hicrm-crm-web/. ${basePath}/hicrm-crm-web
	  # cp -rf ${basePath}/update/hicrm-crm-wechat/. ${basePath}/hicrm-crm-wechat
	  # echo "==> web页面和webChat页面程序已经更新完成。"
   fi
}



# 如果nginx进程存在，则停止nginx进程
# nginxPids=`ps -ef | grep nginx | grep -v grep`
# if [ "$nginxPids" != "" ]; then
#    cd /usr/local/nginx/sbin && ./nginx -s stop
#    echo "==> The nginx process has been stopped."
# fi

# 获取命令行参数
if [ $# -gt 0 ];then
   # 遍历所有命令行参数
   for param in "$@"
   do
     if [ "$param" = "platform" ];then
        runProcess 7090       # 启动 platform 模块进程
     elif [ "$param" = "ureport" ];then
        runProcess 7091       # 启动 ureport 模块进程
     else
       echo "==> Invalid command line parameter:$param"
     fi
   done


   # 启动 nginx
   # echo "========> The nginx have been run. :)"
   # cd /usr/local/nginx/sbin && ./nginx

else
  runProcess  # 启动所有模块进程

  # 启动 nginx
  # echo "========> The nginx have been run. :)"
  # cd /usr/local/nginx/sbin && ./nginx

fi

